package com.enki.cigarettes.utils;

import com.enki.cigarettes.utils.qiniu.QiNiuConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.ResourceUtils;

import javax.annotation.PostConstruct;
import java.io.File;
import java.io.FileNotFoundException;

/**
 * @author WCS.Wang
 * @version V1.0
 * @Package com.enki.cigarettes.utils
 * @Name e-cigarettes-shop-api-module
 * @Description: TODO 七牛工具类
 * @date 2019-08-16
 */
@Component
public class FileUtils {

    @Autowired
    QiNiuConfig staticQiNiuConfig;

    private static QiNiuConfig qiNiuConfig;

    @PostConstruct
    public void init() {
        qiNiuConfig = staticQiNiuConfig;
    }


    /**
     * 检测目录是否存在
     *
     * @return
     */
    public static String isExistDir() {
        try {
            String filepath = "qrcode";
            String realFilepath = ResourceUtils.getFile("classpath:resources") + File.separator + filepath + File.separator;
            File directory = new File(realFilepath);
            if (!directory.exists()) {
                directory.mkdirs();
            } else if (!directory.isDirectory()) {
                directory.mkdirs();
            }
            return realFilepath;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return "";
    }


//    /**
//     * 上传图片
//     *
//     * @param key
//     * @return
//     * @throws Exception
//     */
//    public static String uploadFile(String key) throws Exception {
//        // 生成二维码信息
//        String fileName = key + ".png";
//        String pathName = FileUtils.isExistDir() + fileName;
//        String qrCodeLocalUrl = QRCodeUtils.generateQRCode(key, pathName);
//        // 上传图片到七牛
//        QiNiuUtils.uploadFile(qrCodeLocalUrl, fileName);
//        String qrCodeUrl = qiNiuConfig.getImghost() + fileName;
//        return qrCodeUrl;
//    }

}
